<?php

namespace backend\modules\config\controllers;

use backend\extensions\ApiBaseController;
use backend\services\captcha\CaptchaService;
use backend\services\config\AdminService;

class AdminController extends ApiBaseController
{

    /**
     * 图形验证码
     * **/
    public function actionCaptcha()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['uuid'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        $data = [
            'base64' => CaptchaService::getCaptcha($this->params['uuid'])
        ];
        return $this->jsonSuccess($data);
    }

    /**
     * 后台登录
     * * */
    public function actionLogin()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['account', 'pass', 'code', 'uuid'], 'required', 'message' => '{attribute}属必填项'],
        ];
        
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return AdminService::login($this->params);
    }

    /**
     * 退出登录
     * * */

    public function actionLogout() {
        return AdminService::logout();
    }

    /**
     * 获取权限
     * **/
    public function actionGetMenu()
    {
        return AdminService::getMenu();
    }


    /**
     * 列表
     * * */
    public function actionList()
    {
        $params = \Yii::$app->request->get();
        $rules = [
            [['name', 'account'], 'default', 'value' => 0, 'message' => '检索条件'],
            [['page'], 'default', 'value' => 1, 'message' => '页数'],
            [['page_size'], 'default', 'value' => 30, 'message' => '每页显示总数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        $data = AdminService::getList($this->params);
        return $this->jsonSuccess($data);
    }

    /**
     * 新增
     * * */
    public function actionSave()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['name', 'role', 'account'], 'required', 'message' => '{attribute}属必填项'],
            [['id', 'pass'], 'default', 'value' => '', 'message' => '非必填参数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        if ($this->params['id']) {
            return AdminService::update($this->params);
        }
        unset($this->params['id']);
        return AdminService::create($this->params);
    }

    /**
     * 用户详情
     * * */
    public function actionDetail()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return AdminService::detail($this->params);
    }

    /**
     * 删除用户
     * * */
    public function actionDel()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return AdminService::delete($this->params);
    }


    /**
     * 导出
     * **/
    public function actionExport()
    {
        $params = \Yii::$app->request->get();
        $rules = [
            [['name'], 'default', 'value' => 0, 'message' => '检索条件'],
            [['page'], 'default', 'value' => 1, 'message' => '页数'],
            [['page_size'], 'default', 'value' => 100000, 'message' => '每页显示总数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        $data = AdminService::getList($this->params);
        return AdminService::Export($data['list']);
    }

    /**
     * 导出
     * **/
    public function actionImport()
    {
        return AdminService::Import(\yii\web\UploadedFile::getInstanceByName('file'));
    }
}
